package tech.dongliang.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 代码生成器演示
 * </p>
 */
public class MybatisGenerator {

    public void gen(String packages, String project, String... tables) throws IOException {
        String out = new File("").getCanonicalPath();
        String controllerProjectName = project;

        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig()
                .setDbType(DbType.MYSQL)// 数据库类型
                .setDriverName("com.mysql.jdbc.Driver")
                .setUsername("fanslink")
                .setPassword("Jdc#Pa55")
                .setUrl("jdbc:mysql://39.104.82.218:33006/fanslink_ex?allowMultiQueries=true");
        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig()
                .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
                .setInclude(tables) // 需要生成的表
                .setSuperEntityClass("tech.tech.dongliang.tech.tech.dongliang.common.model.CommonModel")
                .setSuperControllerClass("tech.tech.dongliang.tech.tech.dongliang.common.model.CommonController")
                .setRestControllerStyle(true)
                .setEntityBuilderModel(true)
                .setEntityLombokModel(true);
        PackageConfig packageConfig = new PackageConfig()
                .setModuleName(packages)
                .setParent("tech.tech.dongliang")// 自定义包路径
                .setController("controller")// 这里是控制器包名，默认 web
                .setMapper("dao").setService("service").setEntity("model");
        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig()
                .setFileOverride(true)// 是否覆盖文件
                .setActiveRecord(false)// 开启 activeRecord 模式
                .setEnableCache(false)// XML 二级缓存
                .setBaseResultMap(false)// XML ResultMap
                .setBaseColumnList(false)// XML columList
                .setAuthor("xdzhang")
                .setMapperName("%sDao")
                .setServiceName("I%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController");
        //接口及Model生成配置
        TemplateConfig interfaceTemp = new TemplateConfig().setXml(null).setController(null).setMapper(null).setServiceImpl(null);
        //service生成配置
        TemplateConfig serviceTemp = new TemplateConfig().setXml(null).setEntity(null).setController(null).setService(null);
        //controller生成配置
        TemplateConfig controllerTemp = new TemplateConfig().setXml(null).setEntity(null).setMapper(null).setServiceImpl(null).setService(null);
        //接口及Model生成路径
        String interfaceTarget = out + File.separator + "interface-" + packages + File.separator + "src" + File.separator + "main" + File.separator + "java";
        //service生成路径
        String serviceTarget = out + File.separator + "service-" + packages + File.separator + "src" + File.separator + "main" + File.separator + "java";
        //controller生成路径
        String controllerTarget = out + File.separator + controllerProjectName + File.separator + "src" + File.separator + "main" + File.separator + "java";
        globalConfig.setOutputDir(interfaceTarget);
        new AutoGenerator()
                .setDataSource(dataSourceConfig)
                .setTemplateEngine(new BeetlTemplateEngine())
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(interfaceTemp)
                .setGlobalConfig(globalConfig)
                .execute();
        globalConfig.setOutputDir(serviceTarget);
        new AutoGenerator()
                .setDataSource(dataSourceConfig)
                .setTemplateEngine(new BeetlTemplateEngine())
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(serviceTemp)
                .setGlobalConfig(globalConfig)
                .execute();
        globalConfig.setOutputDir(controllerTarget);
        new AutoGenerator()
                .setDataSource(dataSourceConfig)
                .setTemplateEngine(new BeetlTemplateEngine())
                .setStrategy(strategyConfig)
                .setPackageInfo(packageConfig)
                .setTemplate(controllerTemp)
                .setGlobalConfig(globalConfig)
                .execute();
    }

    /**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    public static void main(String[] args) throws IOException {
        /**
         * packages表示生成的代码所在子包
         * project表示生成的controller在哪个模块
         */
        new MybatisGenerator().gen("system", "web-backend",
                "sys_dept",
                "sys_dict",
                "sys_log",
                "sys_menu",
                "sys_role",
                "sys_role_menu",
                "sys_user",
                "sys_user_role"
        );
    }
}
